var app = getApp();
Page({
  data: {
    city: '北京市',
    type: 1,
    dataList: [],
    x:'',
    y: '',
    page: 1,
    pagesize: 10,
    hasMoreData: true
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      x:options.x,
      y:options.y,
      page: 1,
      city: app.globalData.currentCity,
    })
    this.loadData()
  },
  onShow: function () {
  },
  loadData() {
    var self = this
    app.request({
      url: '/home/api/hotelAround',
      method: 'POST',
      data: {
        city: self.data.city,
        type: self.data.type,
        map_x: self.data.x,
        map_y: self.data.y,
        page: self.data.page,
        pagesize: self.data.pagesize
      },
      success: (res) => {
        var listTemp = self.data.dataList
        if (self.data.page == 1) {
          listTemp = []
          self.setData({
            dataList: []
          })
        }
        if (res.result != null && res.result.length > 0) {
          var list = res.result
          if (list.length < self.data.pagesize) {
            self.setData({
              dataList: listTemp.concat(list),
              hasMoreData: false
            })
          } else {
            self.setData({
              dataList: listTemp.concat(list),
              hasMoreData: true,
              page: self.data.page + 1
            })
          }
        }
      }
    })
  },
  changetype(e) {
    var val = e.currentTarget.dataset.type
    this.setData({
      page: 1,
      type: val
    })
    this.loadData()
  },
  //下拉
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.loadData()
    wx.stopPullDownRefresh();
  },
  //上拉
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.loadData()
    } else {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
    }
  }
})